﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Zo.ExcelBox.Base;
using Zo.ExcelBox.Models;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Zo.ExcelBox.ViewModels
{

    /// <summary>
    /// 
    /// </summary>
    public partial class DataTransformFolderSetWinVM : ObservableObject
    {

        /// <summary>
        /// 
        /// </summary>
        private ObservableCollection<DataTransformFolder> _dataTransforms = new ObservableCollection<DataTransformFolder>();


        /// <summary>
        /// 文件夹目录
        /// </summary>
        public ObservableCollection<DataTransformFolder> DataTransforms
        {
            get => _dataTransforms;
            set => SetProperty(ref _dataTransforms, value);
        }


        /// <summary>
        /// 匹配类型
        /// </summary>
        public ObservableCollection<MatchType> MatchOptions { get; set; } = new ObservableCollection<MatchType>();

        private int _MatchOptionsSelectedIndex;
        public int MatchOptionsSelectedIndex
        {
            get => _MatchOptionsSelectedIndex;
            set => SetProperty(ref _MatchOptionsSelectedIndex, value);
        }

        public ObservableCollection<MapTemplate> MapTemplates { get; set; } = new ObservableCollection<MapTemplate>();



        public TemplateType SelectedOption { get; set; }


        /// <summary>
        /// 添加行
        /// </summary>
        public IRelayCommand OnAddRowCommand { get; }


        /// <summary>
        /// 删除行
        /// </summary>
        public IRelayCommand<DataTransformFolder> OnDeleteRowCommand { get; }

   

        /// <summary>
        /// 重置数据
        /// </summary>
        public IRelayCommand OnResetCommand { get; }


        /// <summary>
        /// 提交数据
        /// </summary>
        public IRelayCommand OnSubmitCommand { get; }


        /// <summary>
        /// 选择文件目录
        /// </summary>
        public IRelayCommand<DataTransformFolder> OnSelectFolderCommand { get; }

        private static string _lastFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);


        /// <summary>
        /// 成功
        /// </summary>
        public event Action<ObservableCollection<DataTransformFolder>> OnSucceed;

        /// <summary>
        /// 数据访问
        /// </summary>
        public static SqlSugarClient _db;

        public DataTransformFolderSetWinVM(ObservableCollection<DataTransformFolder> datas = null)
        {


            _db = new DbContext().DataBase;

            OnAddRowCommand = new RelayCommand(OnAddRow);
            OnDeleteRowCommand = new RelayCommand<DataTransformFolder>(OnDeleteRow);
            OnResetCommand = new RelayCommand(OnReset);
            OnSubmitCommand = new RelayCommand(OnSubmit);
            OnSelectFolderCommand = new RelayCommand<DataTransformFolder>(OnSelectFolder);

            LoadData(datas);
        }


        void LoadData(ObservableCollection<DataTransformFolder> datas)
        {
            var maps = _db.Queryable<MapTemplate>().ToList();
            MapTemplates = new ObservableCollection<MapTemplate>(maps);

            if (datas != null) 
            {
                DataTransforms = new ObservableCollection<DataTransformFolder>(datas);
            }

        }


        /// <summary>
        /// 选择文件目录
        /// </summary>
        /// <param name="row"></param>
        void OnSelectFolder(DataTransformFolder row)
        {
            using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                dialog.Description = "请选择一个文件夹";
                dialog.ShowNewFolderButton = false;
                // 设置初始路径为上一次选择的路径
                dialog.SelectedPath = _lastFolderPath;

                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    row.FolderPath = dialog.SelectedPath;

                    // 记录这一次的路径
                    _lastFolderPath = dialog.SelectedPath;
                }
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        void OnAddRow()
        {
            DataTransforms.Add(new DataTransformFolder());
        }

        /// <summary>
        /// 删除整个文件夹
        /// </summary>
        /// <param name="row"></param>
        void OnDeleteRow(DataTransformFolder row)
        {
            if (row != null)
            {
                DataTransforms.Remove(row);
            }
        }




        /// <summary>
        /// 重置数据
        /// </summary>
        void OnReset()
        {
            DataTransforms.Clear();
        }

        /// <summary>
        /// 提交
        /// </summary>
        void OnSubmit()
        {
            var p = DataTransforms;

            OnSucceed?.Invoke(DataTransforms);
        }


    }


    /// <summary>
    /// 设置文件夹搜索文件跟模版配置
    /// </summary>
    public class DataTransformFolder : ObservableObject
    {

        private string folderPath = string.Empty;

        /// <summary>
        /// 文件夹目录
        /// </summary>
        public string FolderPath
        {
            get => folderPath;
            set => SetProperty(ref folderPath, value);
        }

        /// <summary>
        /// 选择的模版
        /// </summary>
        public TemplateType SelectMapTemplate { get; set; }

    }

}
